package cn.wolfcode.p2p.job;

import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

@Component
public class BidRequestJob implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IPaymentScheduleService paymentScheduleService;
    private ConcurrentLinkedQueue<BidRequest> queue = new ConcurrentLinkedQueue<BidRequest>();
    private ConcurrentLinkedQueue<PaymentSchedule> queuePaymentSchedule = new ConcurrentLinkedQueue<PaymentSchedule>();

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void doQuery(){
        //查询最近一小时要发布的标
        List<BidRequest> bidRequests = bidRequestService.queryPendingBidRequestListInOneHour();
        for (BidRequest bidRequest : bidRequests) {
            queue.add(bidRequest);
        }
        //查询最近一小时要过了招标截止时间,但还没招满的标
        List<BidRequest> passBidRequests = bidRequestService.queryPassDisableDateBidRequestListInOneHour();
        for (BidRequest passBidRequest : passBidRequests) {
            queue.add(passBidRequest);
        }
        System.out.println("query查询" + queue);
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void doQuery2(){
        //查询前一天还款逾期的集合
        List<PaymentSchedule> paymentSchedules =  paymentScheduleService.queryYesterdayOverduePaymentScheduleList();
        for (PaymentSchedule paymentSchedule : paymentSchedules) {
            queuePaymentSchedule.add(paymentSchedule);
        }
        System.out.println("查询逾期"+ queuePaymentSchedule);

        Iterator<PaymentSchedule> iterator = queuePaymentSchedule.iterator();
        //修改状态
        while (iterator.hasNext()) {
            PaymentSchedule paymentSchedule = iterator.next();
            if (paymentSchedule.getState() == Constants.PAYMENT_STATE_NORMAL) {
                iterator.remove();
                //设置还款计划的状态为逾期
                paymentSchedule.setState(Constants.PAYMENT_STATE_OVERDUE);
                //借款对象状态设置为逾期
                BidRequest bidRequest = bidRequestService.get(paymentSchedule.getBidRequestId());
                bidRequest.setBidRequestState(Constants.BIDREQUEST_STATE_PAY_BACK_OVERDUE);
                //更新
                bidRequestService.update(bidRequest);
                paymentScheduleService.update(paymentSchedule);
            }

        }
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void doWork(){
        Iterator<BidRequest> iterator = queue.iterator();
        Date now = new Date();
        while (iterator.hasNext()){
            BidRequest bidRequest = iterator.next();
            if (now.getTime() - bidRequest.getPublishTime().getTime() >= 0
                    && now.getTime() - bidRequest.getDisableDate().getTime() < 0) {

                //可以发标了
                iterator.remove();
                if (bidRequest.getBidRequestState() == Constants.BIDREQUEST_STATE_PUBLISH_PENDING) { //待发标
                    bidRequest.setBidRequestState(Constants.BIDREQUEST_STATE_BIDDING);
                    bidRequestService.update(bidRequest);
                }
            } else if (now.getTime() - bidRequest.getDisableDate().getTime() >= 0){
                //流标
                iterator.remove();
                if (bidRequest.getBidRequestState() == Constants.BIDREQUEST_STATE_BIDDING) {
                    //流标
                    System.out.println("ddddd");
                    bidRequestService.failureOfBid(bidRequest);
                }
            }
        }
        System.out.println("doWork查询" + queue);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        doQuery();
        doQuery2();
    }
}
